package datastructurs.tree;

public class ArrayBinaryTree {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7};
        ArrayTree arrayTree = new ArrayTree(arr);
        arrayTree.preOrder();
    }
}
//实现存储二叉树
class ArrayTree{
    //存储数据节点数组
    private int[] arr;

    public ArrayTree(int[] arr) {
        this.arr = arr;
    }
    public void preOrder(){
        this.preOrder(0);
    }
    //前序遍历
    public void preOrder(int index){
        if(arr==null||arr.length==0){
            System.out.println("Arr is null!");
        }
        System.out.println(arr[index]);
        //向左递归
        if((index*2+1)<arr.length){
            preOrder(2*index+1);
        }
        //向右递归
        if((index*2+2)<arr.length){
            preOrder(2*index+2);
        }
    }
}
